/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelNomina.java
 *
 * Created on Apr 9, 2011, 3:59:25 PM
 */

package frontera.interfaz.panel;

import com.itextpdf.text.DocumentException;
import control.GenerarReporteDeNomina;
import control.SIRHApp;
import entidad.Cargo;
import entidad.Empleado;
import entidad.Prestacion;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;


/**
 *
 * @author tuareg
 */
public class PanelNomina extends javax.swing.JPanel {

    /** Creates new form PanelNomina */
    public PanelNomina() {
        initComponents();
        setBoxModel();        
    }

    private void setBoxModel(){
        minFecha=SIRHApp.getInstance().getAdmCambios().getMinimaFecha();
        int minAño=minFecha.get(Calendar.YEAR);
        Calendar cal = Calendar.getInstance();
        int diferencia = cal.get(Calendar.YEAR) - minAño+1;
        arregloBoxAño = new String[diferencia];
        for(int i=minAño;i<=cal.get(Calendar.YEAR);i++){
            arregloBoxAño[i-minAño]=Integer.toString(i);
        }
        DefaultComboBoxModel año = new DefaultComboBoxModel(arregloBoxAño);
        boxAño.setModel(año);
        boxAño.setSelectedIndex(diferencia-1);

        setBoxModelQuincena(cal.get(Calendar.MONTH),cal.get(Calendar.YEAR));
        setBoxModelMes(cal.get(Calendar.YEAR));
        boxMes.setSelectedIndex(cal.get(Calendar.MONTH));
    }

    private void setBoxModelQuincena(int mes, int año){
        Calendar cal = Calendar.getInstance();
        String quincenaSeleccionada = "1";
        String[] arregloTMP;
        if(año==minFecha.get(Calendar.YEAR)&&minFecha.get(Calendar.DATE)>15){
            arregloTMP = new String[1];
            arregloTMP[0]="2";
            quincenaSeleccionada = "2";
        }
        else if(año == cal.get(Calendar.YEAR) && mes == cal.get(Calendar.MONTH)
            &&cal.get(Calendar.DATE)<=15){
            arregloTMP = new String[1];
            arregloTMP[0]="1";
        }
        else{
            arregloTMP = new String[2];
            arregloTMP[0]="1";
            arregloTMP[1]="2";
            if ( cal.get(Calendar.DATE) > 15 && mes == cal.get(Calendar.MONTH)) {
                quincenaSeleccionada = "2";
            }
        }
        DefaultComboBoxModel quincena = new DefaultComboBoxModel(arregloTMP);
        boxQuincena.setModel(quincena);
        boxQuincena.setSelectedItem(quincenaSeleccionada);
        
    }

    private void setBoxModelMes(int año){
        Calendar cal = Calendar.getInstance();
        int minMes,maxMes;
        if(año==minFecha.get(Calendar.YEAR)){
            minMes = minFecha.get(Calendar.MONTH);    
        }
        else{
            minMes = 0;
        }
        if(año==cal.get(Calendar.YEAR)){
            maxMes = cal.get(Calendar.MONTH);
        }
        else{
            maxMes=11;
        }


        String[] arregloTMP = new String[maxMes-minMes+1];
        for(int i=0; i<=(maxMes-minMes);i++){
            arregloTMP[i]=arregloBoxMes[i+minMes];
        }
        DefaultComboBoxModel mes = new DefaultComboBoxModel(arregloTMP);
        boxMes.setModel(mes);
        boxMes.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        boxMes = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        boxAño = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        boxQuincena = new javax.swing.JComboBox();
        botonAceptar = new javax.swing.JButton();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Invoice.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel3.setText("Seleccionando Fecha");

        jLabel4.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel4.setText("del Reporte");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Selector Fecha"));

        jLabel2.setText("Mes");

        boxMes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxMesActionPerformed(evt);
            }
        });

        jLabel5.setText("Año");

        boxAño.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxAñoActionPerformed(evt);
            }
        });

        jLabel6.setText("Quincena");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(boxAño, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(boxMes, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(boxQuincena, 0, 93, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(boxAño, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boxQuincena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boxMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonAceptar.setText("Generar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addGap(217, 217, 217))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(323, Short.MAX_VALUE)
                .addComponent(botonAceptar)
                .addGap(295, 295, 295))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(137, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(83, 83, 83))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                .addGap(93, 93, 93)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(botonAceptar)
                .addContainerGap(151, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private int indiceDelMes(String mes){
        for(int i=0;i<arregloBoxMes.length;i++){
            if(mes.equals(arregloBoxMes[i])){
                return i;
            }
        }
        return -1;
    }

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt((String)boxAño.getSelectedItem()),
                indiceDelMes((String)boxMes.getSelectedItem()),
                1);
        int dia;
        if(((String)boxQuincena.getSelectedItem()).equals("1")){
            dia = 15;
        }
        else{
            dia = cal.getActualMaximum(Calendar.DATE);
        }

        Calendar fecha = new GregorianCalendar( Integer.parseInt((String)boxAño.getSelectedItem()),
                                                indiceDelMes((String)boxMes.getSelectedItem()),
                                                dia);

        ArrayList<Empleado> arrayEmpleados = SIRHApp.getInstance().getAdmCambios().getEmpleadosFecha(fecha);
        ArrayList<Prestacion> arrayPrestaciones = SIRHApp.getInstance().getAdmCambios().getPrestacionFecha(fecha);
        ArrayList<Cargo> arrayCargos = SIRHApp.getInstance().getAdmCambios().getCargosFecha(fecha);
        
        
//        System.out.println(arrayEmpleados.size());
//        for(Empleado emp:arrayEmpleados){
//            System.out.println("--");
//            System.out.println(emp.getNombre());
//            System.out.println(emp.getFallas().size());
//        }
//        
//        for(Prestacion pre:arrayPrestaciones){
//            System.out.println("--");
//            System.out.println(pre.getNombre());
//        }
//        
//        System.out.println(arrayCargos.size());
//        for(Cargo ca:arrayCargos){
//            System.out.println("--");
//            System.out.println(ca.getNombre());
//        }
        
        
        GenerarReporteDeNomina gen = new GenerarReporteDeNomina();

        try {
            gen.algoritmoReporteDeNomina(fecha, Integer.parseInt(String.valueOf(boxQuincena.getSelectedItem()))-1, arrayEmpleados,
                    arrayPrestaciones, arrayCargos);
        } catch (DocumentException ex) {
            Logger.getLogger(PanelNomina.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PanelNomina.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_botonAceptarActionPerformed

    private void boxAñoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxAñoActionPerformed
        setBoxModelMes(Integer.parseInt((String)boxAño.getSelectedItem()));
    }//GEN-LAST:event_boxAñoActionPerformed

    private void boxMesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxMesActionPerformed
        setBoxModelQuincena(indiceDelMes((String)boxMes.getSelectedItem()),
                            Integer.parseInt((String)boxAño.getSelectedItem()));
    }//GEN-LAST:event_boxMesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JComboBox boxAño;
    private javax.swing.JComboBox boxMes;
    private javax.swing.JComboBox boxQuincena;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private String[] arregloBoxAño;
    private String[] arregloBoxMes={"Enero","Febrero","Marzo","Abril","Mayo","Junio",
                                    "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"};
    private Calendar minFecha;

}
